package br.com.dev.tcc.view.sinistro.chain;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import br.com.dev.tcc.R;

public abstract class SinistroChain {

	private static Animation inToLeftAnim;
	private static Animation outToLeftAnim;
	private static Animation inToRightAnim;
	private static Animation outToRightAnim;

	private int currentChainItenIndex;
	private ViewGroup container;
	private View btnBack;
	private View btnCancel;
	private Button btnNext;
	private SinistroChainItem[] views;
	private OnCancelListener cancelListener;
	private OnFinishListener finishListener;
	private InputMethodManager inputMethodManager;

	public SinistroChain(Activity activity, SinistroChainItem... itens) {
		this.views = itens;
		initComponents(activity);
		setButtons();
		setListeners();
		addViews();
	}

	private void initComponents(Activity activity) {

		inputMethodManager = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);

		container = (ViewGroup) activity.findViewById(R.id.container);
		btnBack = activity.findViewById(R.id.btnBack);
		btnCancel = activity.findViewById(R.id.btnCancel);
		btnNext = (Button) activity.findViewById(R.id.btnNext);

		inToLeftAnim = AnimationUtils.loadAnimation(activity,
				R.anim.chain_in_left);
		outToLeftAnim = AnimationUtils.loadAnimation(activity,
				R.anim.chain_out_left);
		inToRightAnim = AnimationUtils.loadAnimation(activity,
				R.anim.chain_in_right);
		outToRightAnim = AnimationUtils.loadAnimation(activity,
				R.anim.chain_out_right);
	}

	private void setButtons() {

		if (currentChainItenIndex == 0)
			btnBack.setVisibility(View.INVISIBLE);
		else
			btnBack.setVisibility(View.VISIBLE);

		if (currentChainItenIndex == views.length - 1)

			btnNext.setText(R.string.finish);
		else
			btnNext.setText(R.string.next);
	}

	public void setOnCancelListener(OnCancelListener listener) {
		cancelListener = listener;
	}

	public void setOnFinishListener(OnFinishListener listener) {
		finishListener = listener;
	}

	private void setListeners() {

		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showPreviousView();
				setButtons();
			}
		});

		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (views[currentChainItenIndex].isValid()) {
					views[currentChainItenIndex].setSinistroFields();
					if (currentChainItenIndex == views.length - 1)
						finishListener.onFinish();
					else {
						showNextView();
						setButtons();
					}
				}
			}
		});

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cancelListener.onCancel();
			}
		});
	}

	private void addViews() {

		View v = views[0].getView();
		container.addView(v);

		for (int i = 1; i < views.length; i++) {
			v = views[i].getView();
			v.setVisibility(View.GONE);
			container.addView(v);
		}
	}

	private void showPreviousView() {

		View out = views[currentChainItenIndex].getView();
		View in = views[--currentChainItenIndex].getView();

		try{
			inputMethodManager.hideSoftInputFromWindow(out.getWindowToken(), 0);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		out.startAnimation(outToRightAnim);
		in.startAnimation(inToRightAnim);

		in.setVisibility(View.VISIBLE);
		in.setFocusable(true);
		in.setFocusableInTouchMode(true);
		in.setClickable(true);
		in.bringToFront();
		container.requestLayout();
		container.clearFocus();
		container.requestFocus();
		views[currentChainItenIndex].getFirstView().requestFocus();
		out.setFocusable(false);
		out.setFocusableInTouchMode(false);
		out.setClickable(false);
		out.setVisibility(View.GONE);
		views[currentChainItenIndex].onShow();
	}

	private void showNextView() {

		View out = views[currentChainItenIndex].getView();
		View in = views[++currentChainItenIndex].getView();

		out.startAnimation(outToLeftAnim);
		in.startAnimation(inToLeftAnim);

		in.setVisibility(View.VISIBLE);
		in.setFocusable(true);
		in.setFocusableInTouchMode(true);
		in.setClickable(true);
		in.bringToFront();
		container.requestLayout();
		container.clearFocus();
		container.requestFocus();
		views[currentChainItenIndex].getFirstView().requestFocus();
		out.setFocusable(false);
		out.setFocusableInTouchMode(false);
		out.setClickable(false);
		out.setVisibility(View.GONE);
		views[currentChainItenIndex].onShow();
	}

	public interface OnCancelListener {
		public void onCancel();
	}

	public interface OnFinishListener {
		public void onFinish();
	}
}
